/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : my_home.js                            
*  Description  : 마이홈
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-06-25  김효순    Created.  
*******************************************************************************/

jQuery(function($){
	// 부킹이벤트
	function clsEventList()
	{
		this._HTMLDom = null;
		this.listData = null;
		
		this._init();
	}
	
	clsEventList.prototype = {
		_init: function(){
			var $layer = $("div#container table.list01");
			var $img1 = $layer.find("img[alt='당첨자 확인']:first");
			$layer.find("tbody tr:first td:eq(7)").empty().append($img1);
			
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("tbody tr:first").clone(true)
			}
			
			this.setList([]);
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setList: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			$layer.find("tbody").empty();
			for(var i=0;i<this.listData.length;i++){
				$layer.find("tbody").append($element.clone(true));
			}
			
			$layer.find("tbody tr").each(function(n){
				var d = thisClass.listData[n];
				var s = "참여중";
				if(d["상태코드"] == "E" || d["상태코드"] == "F") s = "이벤트 종료";
				
				$(this).find("td:eq(0)").html(d["시작일"] + " ~ " + d["종료일"]);
				$(this).find("td:eq(1)").html(d["라운드날짜"]);
				$(this).find("td:eq(2)").html(d["시간대"]);
				$(this).find("td:eq(3)").html(d["지역코드"]);
				$(this).find("td:eq(4)").html(d["골프장명"] + " " + d["코스명"]);
				$(this).find("td:eq(5)").html(d["그린피"]);
				//$(this).find("td:eq(5) p:eq(1)").html(d["원가"]);
				$(this).find("td:eq(6)").html(s);
				//$(this).find("td:eq(7)").html(d["당첨코드"]);
			});
		}
		, spinStart: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("tbody").hide();
			if($layer.next("div#tmpSpin").length == 0){
				$layer.after("<div id='tmpSpin' style='position:relative; width:700px; height:100px;' />");
			}
			$layer.next("div#tmpSpin").show().spin();
		}
		, spinEnd: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("tbody").show();
			$layer.next("div#tmpSpin").hide().spin(false);
		}
		, getKey: function(n){
			return this.listData[n]["예약코드"];
		}
	}
	
	
	// 서버통신
	function clsComm()
	{
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			
		}
		// 자선예약
		, getEventList: function(arg){
			var thisClass = this;
			
			//ajax
			var serviceName = "DonationMyMainReservList";
			var options = {
				
			}
			_gf.ajax({
				"sid": "US001"
				, "servicename": serviceName
				, "listcount": arg["페이지당수"]
				, "currpageno": arg["페이지"]
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
					, "결과값": []
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					if(!items.length) items = new Array(items);
					for(var i=0;i<items.length;i++){
						var obj = items[i];
						
						res["결과값"].push({
							"예약코드": _util.nvl(obj["reserveSeq"])
							, "시작일": _util.nvl(obj["startDt"])
							, "종료일": _util.nvl(obj["endDt"])
							, "라운드날짜": _util.nvl(obj["reservDate"])
							, "시간대": _util.nvl(obj["bookingTime"])
							, "지역코드": _util.nvl(obj["region"])
							, "골프장명": _util.nvl(obj["golfClubNm"])
							, "코스명": _util.nvl(obj["courseInfoNm"])
							, "그린피": _util.nvl(obj["greenFee"], 0)
							, "상태코드": _util.nvl(obj["reservStatus"]) // C:진행중, E: 신청마감, F: 종료
							, "나의참여상태": _util.nvl(obj["joinStatus"])
							, "당첨코드": _util.nvl(obj["winnerYn"])
						});
					}
				}
				
				document.objControl.finishEventList(res);
			});
		}
	}
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		this.objEventList = null;
		this.objEventPaging = null;
		
		this._permitStart = null;
	}
	
	clsControl.prototype = {
		_init: function(){
			this._preload();
			
			this.objComm = new clsComm();
			this.objEventList = new clsEventList();
			this.objEventPaging = _gf.makePaging($("div#idEventPaging"), 10, 10);
			
			
			this._beforeStart();
			this._bind();
		}
		, _preload: function(){
			_gf.spinStart();
			
			$("div#container section#my_home").append("<div id='idEventPaging' />");
		}
		, _beforeStart: function(){
			var thisClass = this;
			this._permitStart = {
				
			}
			
			$.ajax({
				url: _ROOT + "/js/mypage_common.js"
				, dataType: "script"
				, async: false
				, success: function(){
					
				}
				, error: function(){
					alert("error");
				}
			});
			
			_util.executeThread(function(){
				for(var k in thisClass._beforeStart){
					if(!thisClass._beforeStart[k]) return false;
				}
				
				return (_gf.getMyInfoData());
			}, function(){
				_gf.spinEnd();
				thisClass._start();
			});
		}
		, _bind: function(){
			var thisClass = this;
			
			// 이벤트
			this.objEventList.$getLayer().on("click", "tbody tr", function(){
				var n = thisClass.objEventList.$getLayer().find("tbody tr").index($(this));
				var k = thisClass.objEventList.getKey(n);
				thisClass.goEventViewPage(k);
			});
			
			// 페이지이동
			$(this.objEventPaging).bind("evtPageClick", function(){
				thisClass.getEventList();
			});
			
			$("div#container section#my_home").on("click", "img[alt='부킹정보 보기']", function(){
				var alt = $(this).attr("alt");
				
				switch(alt){
					case "부킹정보 보기":
						thisClass.goBookingList();
						break;
				}
			});
		}
		, _start: function(){
			// 예약현황
			this.objEventPaging.setPage(1);
			this.getEventList();
		}
		// 예약현황 완료
		, finishEventList: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			this.objEventList.spinEnd();
			
			this.objEventList.setList(ret["결과값"]);
		}
		// 예약현황
		, getEventList: function(){
			this.objEventList.spinStart();
			this.objComm.getEventList({
				"페이지당수": this.objEventPaging.getCountPerPage()
				, "페이지": this.objEventPaging.getPage()
			});
		}
		// 이벤트 상세 이동
		, goEventViewPage: function(seq){
			var szLink = _ROOT + "/booking/event_app.html?q_seq=" + seq;
			$("body").append("<a href='" + szLink + "' id='idPermissionLink' target='_blank'></a>");
			$("a:last").get(0).click();
			$("a:last").remove();
		}
		// 부킹정보 페이지
		, goBookingList: function(){
			var szLink = _ROOT + "/mypage/my_booking.html";
			document.location.href = szLink;
		}
	}
	
	document.objControl = new clsControl();
	document.objControl._init();
});
